package fr.lelouet.ga.examples.travelingSalesman;

import fr.lelouet.ga.reducers.Evaluator;

/**
 * evaluates a travel :
 * <ul>
 * <li>base is length + malus</li>
 * <li>malus is maxlength(of the map)*missing towns</li>
 * <li>value is -base</li>
 * </ul>
 * @author le louet
 */
public class TravelEvaluator implements Evaluator<Travel> {

  @Override
  public double eval(Travel target) {
    if (target.size() == 0) {
      return 0;
    }
    float length = target.length();
    int misses = target.missingTowns().size();
    if (target.get(target.size() - 1) != target.get(0)) {
      misses++;
    }
    length += misses * target.getMap().longestNoInfinite();
    if (length < 0) {
      length = 0;
    }
    return -length;
  }

}
